package cn.hchaojie.ssm.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hchaojie.ssm.domain.AjaxResponse;
import cn.hchaojie.ssm.domain.Post;
import cn.hchaojie.ssm.mapper.PostMapper;

@Service
public class PostService {
	@Autowired
	PostMapper mapper = null;
	
	public List<Post> findAll() {
		List<Post> all = mapper.findAll();
		
		return all;
	}
	
	/**
	 * 
	 * @param page - 第几页，从1开始
	 * @param size
	 * @return
	 */
	public AjaxResponse<Post> findPage(int page, int size) {
		// 当前页数据
		List<Post> all = mapper.findPage((page - 1) * size, size);

		// 一共有多少条数据
		int count =  mapper.findCount();

		AjaxResponse<Post> result = new AjaxResponse<>();
		result.setData(all);
		result.setCount(count);
		
		return result;
	}
	
	/**
	 * 
	 * @param page - 第几页，从1开始
	 * @param size
	 * @return
	 */
	public AjaxResponse<Post> searchPage(String title, Integer cid, String created, int page, int size) {
		if (title != null && title.length() != 0) {
			title = "%" + title + "%";
		}
		
		// 当前页数据
		List<Post> all = mapper.search(title, created, cid,
				(page - 1) * size, size);

		// 一共有多少条数据
		int count =  mapper.searchCount(title, created, cid);

		AjaxResponse<Post> result = new AjaxResponse<>();
		result.setData(all);
		result.setCount(count);
		
		return result;
	}
	
	public void deleteBatch(String[] ids) {
		mapper.deleteBatch(ids);
	}
}
